<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>销量销价异常处理</title>
    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
    <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
    <style type="text/css">
		.ruleCheckbox {vertical-align: middle;zoom:150%;}
	</style>
</head>
<body>
     <div class="easyui-container-fluid">
         <div class="easyui-panel" style="border:none;">
             <div class="nav" id="nav"></div>
             <div class="easyui-panel" style="border:none;">
                 <form id="ff">
                     <div style="margin-bottom:20px;">
                     	<span>
                         <label class="item-label">年份：</label>
                         <input type="text" class="easyui-combobox" name="year" id="year" style="width:100px;"/>
                     	</span>
                     	<span id="monthLabel">
	                         <label class="item-label">月份：</label>
	                         <input type="text" class="easyui-combobox" name="month" id="month" style="width:100px;"/></span>
                     	<span id="weekLabel">
	                         <label class="item-label">周数：</label>
	                         <input type="text" class="easyui-combobox" name="week" id="week" style="width:100px;"/></span>
                     	<label class="item-label">品类：</label>
		                <select id="categorySelect" name="categoryCode" class="easyui-combobox" style="width:100px;"></select> 
                     </div>
                     <div style="margin-bottom:20px;margin-left: 80px;">
                     	<div>
	                     	<div style="margin-left: 10px;margin-bottom: 10px;">
								<div style="font-weight: bold;">单条上报</div>                     		
		                     	<div style="margin-left: 20px;margin-bottom: 10px;">
			                     	<div>
				                     	<input type="checkbox" name="ruleItems" value="H1" class="ruleCheckbox"/>
				                     	1.&nbsp;&nbsp;单条记录销量>=<input data-options="width:40" class="easyui-numberbox" type="text" name="h1_args_1" />
			                     	</div>
		                     	</div>
	                     	</div>
	                     	<div style="margin-left: 10px;margin-bottom: 10px;margin-top: 10px;">
								<div style="font-weight: bold;">多条上报</div>                     		
		                     	<div style="margin-left: 20px;margin-bottom: 3px;">
		                     		<input type="checkbox" name="ruleItems" value="H2" class="ruleCheckbox"/>
		                     		1.&nbsp;&nbsp;同渠道、同型号价格小于等于均价*<input data-options="width:40,min:0,max:1.0,precision:2" class="easyui-numberbox" type="text" name="h2_args_1">
		                     	</div>
		                     	<div style="margin-left: 20px;margin-bottom: 3px;">
		                     		2.&nbsp;&nbsp;售价过高处理
		                     	</div>
		                     	<div style="margin-left: 40px;margin-bottom: 3px;">
		                     		<div style="margin-bottom: 5px;">
		                     			<input type="checkbox" name="ruleItems" value="H3" class="ruleCheckbox"/>
			                     		2.1&nbsp;&nbsp;同渠道、同型号价格大于等于均价*<input data-options="width:40,min:1.0,precision:2" class="easyui-numberbox" type="text" name="h3_args_1">
		                     		</div>
		                     		<div style="margin-left: 20px;margin-bottom: 3px;">
			                     		2.1.1&nbsp;&nbsp;<input data-options="width:40,min:0,max:100" class="easyui-numberbox" type="text" name="h3_args_2" id="h3Args2" style="text-align: right;">%&lt;同型号不同单价的量占比&lt;<input data-options="width:40,min:0,max:100" class="easyui-numberbox" type="text" name="h3_args_3" style="text-align: right;">%（单条记录）
		                     		</div>
		                     		<div style="margin-left: 20px;margin-bottom: 3px;">
			                     		2.1.2&nbsp;&nbsp;同型号不同单价的量占比&lt;=<input data-options="width:40" class="easyui-numberbox" type="text" id="sameh3Args2" style="text-align: right;" disabled="disabled"/>%（单条记录）,
			                     		将单价调整为均价*<input data-options="width:40,min:0,precision:2" class="easyui-numberbox" type="text" name="h3_args_4"/>
		                     		</div>
								</div>
		                     	
		                     	
		                     	<div style="margin-left: 20px;margin-bottom: 3px;">
		                     	</div>
		                     	<div style="margin-left: 20px;margin-bottom: 3px;">
		                     		3.&nbsp;&nbsp;单条记录销量过量
		                     	</div>
		                     	<div style="margin-left: 40px;margin-bottom: 3px;">
		                     		<div style="margin-bottom: 3px;">
		                     			<input type="checkbox" name="ruleItems" value="H4" class="ruleCheckbox"/>
		                     			3.1&nbsp;&nbsp;同渠道、同型号，同比环比增幅同时>=<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h4_args_1" style="text-align: right;">% 
		                     		</div>
		                     		<div style="margin-bottom: 3px;">
		                     			<input type="checkbox" name="ruleItems" value="H5" class="ruleCheckbox"/>
		                     			3.2&nbsp;&nbsp;同渠道、同型号，去年无销售，但环比增幅>=<input data-options="width:50,min:0" class="easyui-numberbox" type="text" name="h5_args_1" style="text-align: right;">%
		                     		</div>
		                     		<div style="margin-bottom: 3px;">
		                     			<input type="checkbox" name="ruleItems" value="H6" class="ruleCheckbox"/>
		                     			3.3&nbsp;&nbsp;上月销量为<input data-options="width:40,min:1" class="easyui-numberbox" type="text" name="h6_args_1" style="text-align: right;">台，增幅>=<input data-options="width:50,min:0" class="easyui-numberbox" type="text" name="h6_args_2" style="text-align: right;">%
		                     		</div>
		                     		<div style="margin-bottom: 3px;">
		                     			<input type="checkbox" name="ruleItems" value="H7" class="ruleCheckbox"/>
		                     			3.4&nbsp;&nbsp;单条记录销量>=<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h7_args_1" style="text-align: right;">台且量为10的整数倍
		                     		</div>
								</div>	                     	
		                     	<div style="margin-left: 20px;margin-bottom: 3px;">
		                     		4.&nbsp;&nbsp;老旧型号
		                     	</div>
		                     	<div style="margin-left: 40px;margin-bottom: 3px;">
		                     		<div style="margin-bottom: 5px;">
		                     			<input type="checkbox" name="ruleItems" value="H8" class="ruleCheckbox"/>
		                     			4.1&nbsp;&nbsp;同渠道、同型号与前2月相比，增幅均大于<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h8_args_1" style="text-align: right;">% 时，且同渠道价格与均价浮动过小时（低于<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h8_args_2" style="text-align: right;">%）
		                     		 </div>
		                     		<div style="margin-bottom: 5px;">
		                     			<input type="checkbox" name="ruleItems" value="H9" class="ruleCheckbox"/>
		                     			4.2&nbsp;&nbsp;同渠道、同型号前2月无销售，当月销量≥<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h9_args_1">，且价格与均价浮动过小时（低于<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h9_args_2" style="text-align: right;">%）
		                     		</div>
								</div>	                     	
		                     	<div style="margin-left: 20px;margin-bottom: 3px;">
		                     			5.&nbsp;&nbsp;高端型号
		                     	</div>
		                     	<div style="margin-left: 40px;margin-bottom: 3px;">
		                     		<div style="margin-bottom: 5px;">
		                     			<input type="checkbox" name="ruleItems" value="H10" class="ruleCheckbox"/>
		                     			5.1&nbsp;&nbsp;价格从高到低排序累计额份额达到<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h10_args_1" style="text-align: right;">%的型号，销量>=<input data-options="width:40,min:0" class="easyui-numberbox" type="text" name="h10_args_2"> 
		                     		</div>
								</div>                     	
	                     	</div>
                     	</div>
                     </div>
                     <div style="padding:5px 0;text-align:center;">
					     <div id="progressbar" class="easyui-progressbar" style="width:400px;margin:0 auto;"></div>
	                </div>
                     <div style="padding:5px 0;text-align:center;">
	                    <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="resetAdjust()" style="width:80px;">撤消</a>
	                    <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="runRule()" style="width:80px;margin-left: 20px;" id="runBtn">调整</a>
	                    <!-- 
	                    <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="disableRule()" style="width:80px;margin-left: 20px;" data-options="disabled:true" id="disableBtn">禁用</a>
	                     -->
	                </div>
                 </form>
             </div>
         </div>
     </div>
     <script type="text/javascript">
     	var _categoryCode;
     	var _year;
     	var _month;
     	var _week;
     	
         $(function(){
             setNav();
             initUserCategorySelect("categorySelect", showRule, function(){
            	 // 默认选中第二个选项
            	 var data = $('#categorySelect').combobox('getData');
            	 $('#categorySelect').combobox('select', data[1].code);
             });
             $.when(initMonthWeek("monthLabel","weekLabel")).done(showRule);
             
             $("input",$("#h3Args2").next("span")).blur(function(){ 
	                $("#sameh3Args2").textbox('setValue', $("#h3Args2").textbox('getValue'));
             })
             aRecords("销量销价异常处理","/webapp/pages/product/audit/salescountpricehandle")
         });
         
         function showRule(){
        	 if(!$('#year').combobox('getValue')){
        		 return;
        	 }
        	 if(window.parent.workMode=='week' && !$('#week').combobox('getValue')){
            	 // 周报
            	 return;
             }else if(window.parent.workMode=='month' && !$('#month').combobox('getValue')){
            	 // 月报
            	 return;
             }
        	 if(!$('#categorySelect').combobox('getValue')){
        		 return;
        	 }
             var data = getFormData(document.getElementById("ff"));
             $.ajax({
                  url:config.url + "/salesCountPriceHandle/rule/get",
                  type:"get",
                  data:transfer(data),
                  xhrFields:{withCredentials:true},
                  beforeSend:function () {
                      $.messager.progress({
                          title:"请稍等",
                          interval:50,
                          text:"数据加载中..."
                      });
                    },
                  success:function(data){
                	  if(data && data.categoryCode){
                		  	_categoryCode = data.categoryCode;
                	     	_year = data.year;
                	    	_month = data.month;
                	     	_week = data.week;
                		  
                		  // 填充表单
                		  $("#ff").form("load",changeProperty2String(data));
                		  $("#sameh3Args2").textbox('setValue', $("#h3Args2").textbox('getValue'));
                		  
                		  $('#progressBar').hide();
                		  if('true'==data.existsTask){
                				// “调整”按钮不可点击
		                        $('#runBtn').linkbutton('disable');
		   	                	$('#runBtn').linkbutton({text:'调整中'});

		                        // “禁用”按钮不可点击
		   	                	$('#disableBtn').linkbutton('disable');
		   	                	$('#disableBtn').linkbutton({text:'禁用'});
		   	                	
		   	                	$('#progressbar').progressbar('setValue', ~~data.progress);
		   	                	showProgress();
                		  }else if(data.entityStatus=='OK'){
	                			// “调整”按钮可点击
                        		$('#runBtn').linkbutton('enable');
   	                			$('#runBtn').linkbutton({text:'调整'});
	                        	
                        		// “禁用”按钮可点击
   	                			$('#disableBtn').linkbutton('enable');
   	                			$('#disableBtn').linkbutton({text:'禁用'});
   	                			
		   	                	$('#progressbar').progressbar('setValue', ~~data.progress);
                		  }else{
	                		 	// “调整”按钮可点击
                        		$('#runBtn').linkbutton('enable');
   	                			$('#runBtn').linkbutton({text:'调整'});
	                        	
                        		// “禁用”按钮不可点击
   	                			$('#disableBtn').linkbutton('disable');
   	                			$('#disableBtn').linkbutton({text:'已禁用'});
                		  }
                	  }
                	  $.messager.progress("close");
                  },
                  error:function(data){
                     console.log(data);
                     $.messager.progress("close");
                  }
              });
          }
         
         function resetAdjust(){
         	if(!$('#categorySelect').combobox('getValue')){
 	       		return;
 	       	}
         	var formData = getFormData(document.getElementById("ff"));
         	$.messager.confirm("提示","您确定要撤消之前的调整吗？",function(flag){
                 if(flag){
                    $.ajax({
                        url:config.url + "/salesCountPriceHandle/adjust/reset",
                        type:'POST',
                        data: formData,
                        xhrFields:{withCredentials:true},
                        success:function(data){
                            $.messager.alert("提示","撤消成功");
                            showRule();
                        },
                        error:function(XMLHttpRequest,textStatus,thrown){
                            $.messager.alert("提示","操作失败","error");
                        }
                    });
                 }
            });
         }
         
         function runRule(){
        	var ruleItemsMap = {
	        	"H1" : ["h1_args_1"], 
	        	"H2" : ["h2_args_1"], 
	        	"H3" : ["h3_args_1", "h3_args_2", "h3_args_3", "h3_args_4"], 
	        	"H4" : ["h4_args_1"], 
	        	"H5" : ["h5_args_1"], 
	        	"H6" : ["h6_args_1", "h6_args_2"], 
	        	"H7" : ["h7_args_1"], 
	        	"H8" : ["h8_args_1", "h8_args_2"], 
	        	"H9" : ["h9_args_1", "h9_args_2"], 
	        	"H10" : ["h10_args_1", "h10_args_2"]
        	}
        	
        	// 获取已被选中的规则项
        	var checkedRuleItems =[]; 
		    $('input[name="ruleItems"]:checked').each(function(){ 
		    	checkedRuleItems.push($(this).val()); 
		    });
		    if(checkedRuleItems.length==0){
		    	$.messager.alert("提示","请先选择规则项","warning");
	        	return;
		    }
		    
		    // 验证已选中的规则项设置的参数是否有效
		    var formData = getFormData(document.getElementById("ff"));
        	var enable = true;
        	for(var r=0;r<checkedRuleItems.length;r++){
        		var fields = ruleItemsMap[checkedRuleItems[r]];
	        	for(var i=0;i<fields.length;i++){
	        		if(!formData[fields[i]] || formData[fields[i]] <= 0){
	        			enable = false;
	        			break;
	        		}
	        	}
        	}
        	
        	if(!enable){
       			$.messager.alert("提示","所选的规则项参数设置无效","warning");
				return;        		
        	}
        	
        	$.messager.confirm("提示","您确定要调整吗？",function(flag){
                if(flag){
		             $("#ff").form("submit",{
		                   onSubmit:function(){
		                     var valid = $(this).form('enableValidation').form('validate');
		                     if(!valid)return false;
		                     var formData = getFormData(document.getElementById("ff"));
		                     if(formData['ruleItems'] && typeof formData['ruleItems'] === 'string'){
		                          formData['ruleItems'] = [formData['ruleItems']]
		                      }
		                     $.ajax({
		                        url:config.url + "/salesCountPriceHandle/rule/run",
		                        type: "POST",
		                        data:JSON.stringify(formData),
		                        dataType:'json',
		                        contentType:'application/json;charset=UTF-8',
		                        xhrFields:{withCredentials:true},
		                        beforeSend:function(){
		                             // 
		                        },
		                        complete:function(){
		                             // 
		                        },
		                        success:function(data){
		                        	// “调整”按钮不可点击
		                        	$('#runBtn').linkbutton('disable');
		   	                		$('#runBtn').linkbutton({text:'调整中'});
		                        	
		                        	// “禁用”按钮不可点击
		   	                		$('#disableBtn').linkbutton('disable');
		   	                		$('#disableBtn').linkbutton({text:'禁用'});
		                            $.messager.alert("提示","任务提交成功，正在调整中。。。","info");
		                            
		                            $('#progressbar').progressbar('setValue', 0);
		                            showProgress();
		                         },
		                         error:function(XMLHttpRequest,textStatus,thrown){
		                        	// “调整”按钮不可点击
			                        $('#runBtn').linkbutton('disable');
			   	                	$('#runBtn').linkbutton({text:'调整中'});
			                        	
			                        // “禁用”按钮不可点击
			   	                	$('#disableBtn').linkbutton('disable');
			   	                	$('#disableBtn').linkbutton({text:'禁用'});
		                            $.messager.alert("提示", "任务正在进行中，请勿重复操作", "warning");
		                         }
		                     });
		                      return false;
		                  }
		             });
               	}
            });
        	
         }
         
        function disableRule(){
        	if(!$('#categorySelect').combobox('getValue')){
	       		return;
	       	}
        	var formData = getFormData(document.getElementById("ff"));
        	$.messager.confirm("提示","您确定禁用该周期的规则吗？",function(flag){
                if(flag){
                   $.ajax({
                       url:config.url + "/salesCountPriceHandle/rule/disable",
                       type:'POST',
                       data: formData,
                       xhrFields:{withCredentials:true},
                       success:function(data){
                           	$.messager.alert("提示","操作成功");
                           	$('#disableBtn').linkbutton('disable');
	                	   	$('#disableBtn').linkbutton({text:'已禁用'});
                       },
                       error:function(XMLHttpRequest,textStatus,thrown){
                           $.messager.alert("提示","操作失败","error");
                       }
                   });
                }
           });
        }
        
        function showProgress(){
        	$.ajax({
                url:config.url + "/salesCountPriceHandle/task/progress/get",
                type:"get",
                data: {'categoryCode': _categoryCode, 'year':_year, 'month':_month, 'week':_week},
                xhrFields:{withCredentials:true},
                success:function(data){
        			if ($('#progressbar').progressbar('getValue') < 100){
        				$('#progressbar').progressbar('setValue', ~~data);
        				setTimeout(showProgress, 2000);
        			}
                },
                error:function(data){
                   console.log(data);
                }
            });
        }
		
	</script>
</body>
</html>